<template>
  <div>
    <h2>测试题</h2>
    <div v-for="(item, index) in arr" :key="index">
      <div>
        <span>{{ item.num1 }}</span>
        <span>+</span>
        <span>{{ item.num2 }}</span>
        <span>=</span>
        <input type="number" v-model="item.result" />
        <button @click.prevent="sum(index)" :disabled="item.submitted">
          提交
        </button>
      </div>
      <br />
    </div>
    <!-- footer -->
    <div>
      <span
        v-for="(item, ind) in arr"
        :key="ind"
        :class="{
          right: item.status === '正确',
          error: item.status === '错误',
          undo: item.status === '未完成',
        }"
        >{{ ind + 1 }}: {{ item.status }}</span
      >
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          status: "未完成",
          result: "",
          submitted: false,
        },
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          status: "未完成",
          result: "",
          submitted: false,
        },
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          status: "未完成",
          result: "",
          submitted: false,
        },
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          status: "未完成",
          result: "",
          submitted: false,
        },
        {
          num1: Math.floor(Math.random() * 10),
          num2: Math.floor(Math.random() * 10),
          status: "未完成",
          result: "",
          submitted: false,
        },
      ],
    };
  },
  methods: {
    sum(index) {
      if (this.arr[index].result == "") {
        return (this.arr[index].status = "未完成");
      }
      if (
        this.arr[index].result ==
        this.arr[index].num1 + this.arr[index].num2
      ) {
        return (this.arr[index].status = "成功");
      } else {
        return (this.arr[index].status = "失败");
      }
    },
  },
};
</script>
<style>

</style>